package com.example.springboot_blog.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot_blog.dao.CommentDao;
import com.example.springboot_blog.dao.RoleDao;
import com.example.springboot_blog.dao.UserDao;
import com.example.springboot_blog.domain.Comment;
import com.example.springboot_blog.domain.User;
import com.example.springboot_blog.service.CommentService;
import com.example.springboot_blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;


    //实现注册功能，根据用户名和邮箱查询当前用户是否已存在
    public boolean register(String email, String username, String password){
        if (userDao.selectByUsername(username) != null) {
            return false;
        }
        else if (userDao.selectByEmail(email) != null){
            return false;
        }
        else {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            User user = new User();
            user.setUsername(username);
            user.setEmail(email);
            user.setPassword(encoder.encode(password));
            user.setActive(true);
            userDao.insert(user);
            return true;
        }
    }

    @Override
    public Boolean deleteById(Integer id) {
        return userDao.deleteById(id)>0;
    }

    @Override
    public List<User> selectUserList(User user) {
        return userDao.selectList(null);
    }
}
